 <?php

class MemberAction extends CommonAction {

	public function index() {

		$this->user = D('User')->relation(true)->select();

		/*foreach ($user as $v) {
			
			foreach ($v as $val) {
				p($val);
			}
		}
		die;*/

		$this->display();
	}

	public function add() {

		$role = M('role')->select();

		//print_array($role);
		
		
		$this->assign('role',$role);
		$this->display();
	}

	public function addHandle() {
		
		
		$w=htmlspecialchars(trim($_POST['user_login']));
        $i=D('user')->where(array('user_login'=>$w))->select();
        if($i!=''){
            $this->error('用户名已存在！');
        }else{
        //组合用户信息并添加
            $user=array(
            'user_login'=>htmlspecialchars(trim($_POST['user_login'])),
            'user_nicename'=>htmlspecialchars(trim($_POST['user_nicename'])),
            'display_name'=>htmlspecialchars(trim($_POST['display_name'])),
            'user_pass'=>md5($_POST['password']),
            'user_email'=>htmlspecialchars($_POST['user_email']),
            'user_url'=>htmlspecialchars($_POST['user_url']),
            'user_intro'=>htmlspecialchars($_POST['user_intro']),
           // 'logintime'=>time(),
           // 'loginip'=>get_client_ip(),
            // 'lock'=>$_POST['lock']
            );
            //添加用户与角色关系
          
            
            $User=D('user');$User_users=D('role_users');
            if($new_id=$User->add($user) ){
            	
            	//echo $User->getLastSql();
            	
            	//echo $User_users ->getLastSql();
            	$role=array(
            			'role_id'=>$_POST['role_id'],
            			'user_id'=>$new_id);
            	if($User_users ->add($role)){
            		$this->success('添加成功！' , U('Admin/Member/index'));
            	}else {
            		$this->error('添加用户权限失败！');
            	}
            }else{
            	$this->error('添加用户失败！');
            }
		}
	}
} 
